<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\PaperConfig as PaperConfigLogic;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class PaperConfig
 *
 * @package app\exam\service\admin
 */
class PaperConfig extends BaseService
{

    /**
     * @Inject()
     *
     * @var PaperConfigLogic
     */
    protected $paperConfigLogic;

    /**
     * set
     *
     * @param string $dataKey
     * @param int    $paperId
     * @return \think\Response
     * @throws \Yansongda\Pay\Exceptions\BusinessException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function set(string $dataKey, int $paperId)
    {
        $cnfData = Request::post();

        unset($cnfData['data_key'], $cnfData['paper_id']);

        $cnfData['exam_stime'] = $cnfData['time'] ? strtotime($cnfData['time'][0]) : 0;
        $cnfData['exam_etime'] = $cnfData['time'] ? strtotime($cnfData['time'][1]) : 0;

        unset($cnfData['time']);

        $this->paperConfigLogic->saveConfig($dataKey, $paperId, $cnfData);

        return $this->parseToData([], 1, '设置成功');
    }

    /**
     * get
     *
     * @param string $dataKey
     * @param int    $paperId
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get(string $dataKey, int $paperId)
    {
        $data = $this->paperConfigLogic->get($dataKey, $paperId);

        $examStime    = (isset($data['exam_stime']) && $data['exam_stime'] > 0) ? date('Y-m-d H:i', $data['exam_stime']) : '';
        $examEtime    = (isset($data['exam_etime']) && $data['exam_etime'] > 0) ? date('Y-m-d H:i', $data['exam_etime']) : '';
        $data['time'] = [];
        if ($examStime && $examEtime) {
            $data['time'] = [$examStime, $examEtime];
        }

        return $this->parseToData($data, 1, '获取成功');
    }
}